package com.cby.leetcode.哈希.LC49;

import java.util.*;

/**
 * ClassName: LC49
 * Package: com.cby.leetcode.哈希.LC49
 * Description:
 *
 * @Author dhu-bion
 * @Create 2025/9/5 9:46
 * @Version 1.0
 */
public class LC49 {
    public static void main(String[] args){

    }

    static class Solution{
        public List<List<String>> groupAnagrams(String[] strs){
            //1.创建hash
            Map<String,List<String>> map = new HashMap<>();

            //2.遍历
            for(String s : strs){
                char[] keyChars = s.toCharArray();
                Arrays.sort(keyChars);
                String key = new String(keyChars);
                // 若 key 不存在，生成一个空的 ArrayList；若存在，直接获取对应的 List
                List<String> list = map.computeIfAbsent(key, k -> new ArrayList<>());
                // 向 List 中添加元素 s
                list.add(s);
            }

            return new ArrayList<>(map.values());
        }
    }
}
